import requests
from lxml import etree
from  datetime import datetime,timedelta


class ExchangeRates():

    def __init__(self):
        # self.send_request()
        self.ratename()


    def send_request(self):
        url = 'https://srh.bankofchina.com/search/whpj/search_cn.jsp'
        header = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36'
        }
        datas = {
            'erectDate':'2022-04-25',
            'nothing':'2022-04-25',
            'pjname':'英镑',
            'page':99,
            'head':'head_620.js',
            'bottom':'bottom_591.js'
        }
        res = requests.post(url=url,headers=header,data=datas)
        # print(res.text)
        elements = etree.HTML(res.text)
        rate = elements.xpath("//div[@class='BOC_main publish']/table/tr[last()-1]/td")
        print(len(rate))
        for r in range(0,len(rate)):
            x = rate[r].xpath('text()')[0].strip()
            print(x)
            print(len(x))
        rate2 = elements.xpath("//div[@class='BOC_main publish']/table/tr[last()-1]/td/text()")[0].replace(" ","")
        print(rate2)


    def ratename(self):
        theday = datetime.now()
        a = (datetime.now()+timedelta(days=-4)).strftime('%F')
        # print(a)
        # print(theday)
        distance = theday.weekday()
        if distance == 0:
            monday = theday
            monday_str = theday.strftime('%F')
        else:
            monday = (theday+timedelta(days=-distance))
            monday_str = monday.strftime('%F')
        # print(monday)
        monday_all = list()
        while True:
            monday_all.append(monday.strftime('%F'))
            monday = monday + timedelta(days=-7)
            if monday.year < 2022:
                break
        print(monday_all)


exchange_rates = ExchangeRates()